package com.smart.stock.module.spider.jobSpiderWeb.csindex;

import com.smart.stock.common.util.file.ExcelOperactionTool;
import com.smart.stock.common.util.file.FileCommonOperactionTool;
import com.smart.stock.module.maindata.entity.CsIndexIndustry;
import com.smart.stock.module.GlobalConstant;
import com.smart.stock.module.spider.util.TradingDateUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;


/**
 *
 * @author chenguoxiang
 * @create 2019-08-21 10:26
 **/
@Slf4j
public class CsindexIndustrySpider implements Callable<List<CsIndexIndustry>> {

    private static String url = GlobalConstant.URL_CSINDEXINDUSTRY;
    private static String referrer = GlobalConstant.URL_CSINDEXINDUSTRY_REFERRER;
    private static String download_folder = GlobalConstant.FILEPATH_CSINDEXINDUSTRY;
    private static String download_file = GlobalConstant.FILEPATH_CSINDEXINDUSTRY_FILE;
    @Override
    public List<CsIndexIndustry> call() throws Exception {
        log.info("开始下载文件：{}",url);
        String path= FileCommonOperactionTool.downloadFile(url, referrer,download_folder, "csindextype.zip");
        log.info("分类表下载路径：{}",path);
        FileCommonOperactionTool.decompressZip(path,download_folder);
        List<Object[]> list = ExcelOperactionTool.readExcelData(download_file);
        Integer dateTime=Integer.valueOf(TradingDateUtil.getDateYYYYMMdd());
        List<CsIndexIndustry> data = new ArrayList<>();
        for (Object[] cell:list){
            String code=cell[1].toString();
            //900 为B股 todo
//            if (!code.matches("\\d{6}")||code.startsWith("900")){
//                continue;
//            }
            data.add(new CsIndexIndustry(
                    code,
                    cell[2].toString().replaceAll("\\s", ""),
                    cell[6].toString(),
                    cell[9].toString(),
                    cell[12].toString(),
                    cell[15].toString(),
                    cell[17].toString(),
                    dateTime
            ));
        }
        return data;
    }


}
